package hivi.vn.hivigold.adapters;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.objects.VoucherDetail;
import hivi.vn.hivigold.utils.Formats;

import java.util.ArrayList;

import org.w3c.dom.Text;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class VoucherAdapter extends BaseAdapter {
	ArrayList<VoucherDetail> arr;
	Context c;

	public VoucherAdapter(Context c, ArrayList<VoucherDetail> arr) {
		// TODO Auto-generated constructor stub
		this.c = c;
		this.arr = arr;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arr.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		View v = arg1;
		ViewHolder vh;
		if(v==null)
		{
			v = ((LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.voucher_detail, null);
			vh = new ViewHolder();
			vh.txtMahang = (TextView) v.findViewById(R.id.txtMavt);
			vh.txtTenhang = (TextView) v.findViewById(R.id.txtVtName);
			vh.txtSoluong = (TextView) v.findViewById(R.id.txtSl);
			vh.txtTlgau = (TextView) v.findViewById(R.id.txtTlgau);
			vh.txtHlgau = (TextView) v.findViewById(R.id.txtHlgau);
			vh.txtTlgda = (TextView) v.findViewById(R.id.txtTlgda);
			vh.txtTongtlg = (TextView) v.findViewById(R.id.txtTongtlg);
			vh.txtTiencong = (TextView) v.findViewById(R.id.txttiencong);
			vh.txtTienda = (TextView) v.findViewById(R.id.txttienda);
			vh.txtGiavang = (TextView) v.findViewById(R.id.txtgiavang);
			vh.txtGiaban = (TextView) v.findViewById(R.id.txtgiaban);
			vh.txtThanhtien = (TextView) v.findViewById(R.id.txtthanhtien);
			v.setTag(vh);
		}
		else
		{
			vh = (ViewHolder)v.getTag();
		}
		VoucherDetail vd = arr.get(arg0);
		vh.txtMahang.setText(vd.getFK_VtID());
		vh.txtTenhang.setText(vd.getVtName());
		vh.txtSoluong.setText(Formats.number(vd.getSo_luong()));
		vh.txtTlgau.setText(Formats.number(vd.getTlg_au()));
		vh.txtHlgau.setText(Formats.number(vd.getHlg_au()));
		vh.txtTlgda.setText(Formats.number(vd.getTlg_da()));
		vh.txtTongtlg.setText(Formats.number(vd.getTong_tlg()));
		vh.txtTiencong.setText(Formats.money(vd.getTien_cong()));
		vh.txtTienda.setText(Formats.money(vd.getTien_da()));
		vh.txtGiavang.setText(Formats.money(vd.getGia_au()));
		vh.txtGiaban.setText(Formats.money(vd.getGia()));
		vh.txtThanhtien.setText(Formats.money_round(vd.getTien()));
		return v;
	}

	public void swapData(ArrayList<VoucherDetail> arr)
	{
		this.arr = arr;
		notifyDataSetChanged();
	}
	
	static class ViewHolder {
		TextView txtMahang;
		TextView txtTenhang;
		TextView txtSoluong;
		TextView txtTlgau;
		TextView txtHlgau;
		TextView txtTlgda;
		TextView txtTongtlg;
		TextView txtTiencong;
		TextView txtTienda;
		TextView txtGiavang;
		TextView txtGiaban;
		TextView txtThanhtien;
	}
}
